package cc.dullsky.toutiao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.Query;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;

import cc.dullsky.common.model.Article;
import cc.dullsky.common.model.Articlein;
import cc.dullsky.common.model.User;
import cc.dullsky.interceptor.AuthInterceptor;
import cc.dullsky.lucene.model.SearchResultBean;
import cc.dullsky.lucene.operation.IndexOperation;
import cc.dullsky.lucene.operation.IndexSearch;
import cc.dullsky.util.RegexpUtil;
import cc.dullsky.util.ToutiaoUtil;

@Before(AuthInterceptor.class)
public class TouTiaoController extends Controller {
	public void index() {
		User user = getSessionAttr("user");
		int userId = user.getId();
		PropKit.use("config.properties");
		String indexName = PropKit.get("indexName");
		IndexOperation index = new IndexOperation(indexName);
		List<Map<String, String>> list = ToutiaoUtil.getFavorites();
		for (Map<String, String> map : list) {
			Articlein articlein = new Articlein();
			articlein.setDomain(map.get("domain"));
			articlein.setRefer(map.get("refer"));
			articlein.setTitle(map.get("title"));
			articlein.setUrl(map.get("url"));
			articlein.setUserId(userId);
			boolean flag = ToutiaoUtil.getArticleByToutiao(articlein.getUrl()) != null;
			if (flag) {
				articlein.setStatus("1");
				Article article = new Article().set("title", articlein.getTitle()).set("createtime", new Date()).set("user_id", userId)
						.set("htmlcontent", ToutiaoUtil.getArticleByToutiao(articlein.getUrl())).set("status", "0");
				article.save();
				Document doc = new Document();
				doc.add(new StringField("id", String.valueOf(article.getId()), Store.YES));
				doc.add(new TextField("title", article.getTitle(), Store.YES));
				doc.add(new TextField("content", RegexpUtil.getTextFormHtml(article.getHtmlcontent()), Store.YES));
				index.addDocument(doc);
			} else {
				articlein.setStatus("0");
			}
			Articlein.dao.saveOrNothing(articlein);
		}
	}

	@Clear
	public void lucene() {
		try {
			String indexName = "dullsky";
			IndexSearch search = new IndexSearch(indexName);
			QueryParser parser = new QueryParser("content", new StandardAnalyzer());
			Query query = parser.parse("小编");
			SearchResultBean bean = search.search(query, 0, 10);
			System.out.println("总条数:" + bean.getCount());
			List<Document> documents = bean.getDocs();
			for (Document document : documents) {
				System.out.println("id:\t" + document.get("id"));
				System.out.println("title:\t" + document.get("title"));
				System.out.println("content:\t" + document.get("content"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
